{extend name="lib/main" /}
{block name="title"}{/block}
{block name="css"}
{load href="/static/plugins/select2/css/select2.css" /}
{load href="/static/plugins/bootstrap-datetimepicker/css/bootstrap-datetimepicker.css" /}
<style>
    .upfile i{padding-right:3px !important;}
    .file{ position:absolute;right:0;top:0;width:100px;height:100px;opacity:0; ilter:alpha(opacity=0);cursor:pointer;margin-bottom: 0px;margin-right: 0px; }
</style>
{/block}
{block name="javascript"}
{load  href="/static/plugins/select2/js/select2.js" /}
{load  href="/static/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.js" /}
{load  href="/static/plugins/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js" /}

<script>
    $(document).ready(function () {
        $("#ok").bind("click", Save);
        $("#cancel").bind("click", function () {
            parent.layer.close(parent.layer_add_index);
        });

        /**
         * 保存内容
         */
        function Save() {
            var load_index = layer.load(0, { shade: false });

            $.ajax({
                type: 'POST',
                url: "{:url('menu/add')}",
                data: $("#fmNew").serialize(),
                dataType: 'json',
                success: function (data) {
                    if (data.code == 1) {
                        layer.close(load_index);
                        layer.alert(data.msg, function () {
                            parent.refreshtb();
                            //window.close();
                            parent.layer.close(parent.layer_add_index);
                        });
                    } else {
                        layer.alert(data.msg);
                    }
                }
            })
        }

        $('#p_id').select2();
    });

    /**
     * 新增图片
     */
    var layer_image_add;
    function menuImg() {
        layer_image_add = layer.open({
            type: 2,
            title: "新增图片",
            area: ['70%', '90%'],
            content: "{:url('upload/image')}?tag=menu_img"
        })
    }

    // 上传成功后的回调，该函数名不能写错
    function getImageUrl(res) {
        if (res.tag == 'menu_img') {
            $('#img_url').val(res.url);
            $('#previewImg_menu').attr('src', res.url);
        }
    }

    function pIdValue() {
        var level = $("#p_id").children('option:selected').attr('level');
        if (level == 1) {
            level = 2;
        } else if(level == 2) {
            level = 3;
        } else {
            level = 1;
        }
        $('#level_disable').val(level);
        $('#level').val(level);
    }
</script>
{/block}

{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form method="post" id="fmNew" class="form-horizontal" enctype="multipart/form-data">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜单名称：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="title" placeholder="请输入名称">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜单图标类：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="icon_class">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜单路由：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="route" placeholder="请输入准确的菜单路由....">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">父级菜单：</label>
                            <div class="col-sm-8">
                                <select class="form-control" name="p_id" id="p_id" onchange="pIdValue()">
                                    <option value="">请选择父级菜单...</option>
                                    {php}
                                        if (!empty($parent_list)) {
                                            foreach ($parent_list as $li) {
                                                if ($li['level'] == 1) {
                                                    echo "<option value=" . $li['id'] . " level=" . $li['level'] . " > " . $li['title'] . "</option>";
                                                    if (!empty($li['child'])) {
                                                        foreach ($li['child'] as $item) {
                                                            echo "<option value=" . $item['id'] . " level=" . $item['level'] . "> &nbsp;&nbsp;&nbsp;&nbsp;>>&nbsp;" . $item['title'] . "</option> ";
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                    {/php}
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <!--<div class="form-group">
                            <label class="col-sm-2 control-label">引导图(750*1160)：</label>

                            <div class="col-sm-1">
                                <span class="btn btn-primary btn-sm" style="margin-bottom:0;padding:6px 8px;" onclick="menuImg()">
                                    <i class="glyphicon glyphicon-folder-open" style="padding-right:3px;"></i> 引导图<input type="hidden" name="img_url" id="img_url" />
                                </span>
                            </div>
                            <div class="col-sm-1">
                                <img id="previewImg_menu" style="height:34px;float: right;">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>-->

                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜单等级：</label>
                            <div class="col-sm-8">
                                <select class="form-control" name="level_disable" id="level_disable" readonly >
                                    {volist name="level_list" id="v"}
                                    <option value="{$key}" disabled>{$v}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜单类型：</label>
                            <div class="col-sm-8">
                                <select class="form-control" name="type">
                                    {volist name="type_list" id="v"}
                                    <option value="{$key}">{$v}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">排序：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="sort" placeholder="越小越靠前....">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态：</label>
                            <div class="col-sm-8">
                                <select class="form-control" name="status" id="status">
                                    {volist name="status_list" id="v"}
                                    <option value="{$key}">{$v}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">描述：</label>
                            <div class="col-sm-8">
                               <textarea name="desc" cols="61" rows="3"></textarea>
                            </div>
                        </div>

                        <div id = 'hidden_input'>
                            <input type="hidden" name="level" id="level" value="1"/>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-12 col-sm-offset-5">
                                <button class="btn btn-primary" id="ok" type="button">提交</button>
                                <button class="btn btn-white" id="cancel" type="button">取消</button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}